﻿using System;
using System.Collections.Generic;
using System.Text;
using SevenZip;
using System.IO;
using LuaBoard;
using Language;

namespace Archiver
{
    public static class C7Z
    {
        public static LuaBoard.LuaBoard MainForm;
        public static int volumeSize = 0;
        public static CompressionLevel compressionLevel = CompressionLevel.High;
        private static LuaBoard.ProgressForm progress;
        private static LanguageData lang = Language.CommLanguageData.data;

        public static string FileCompressionStarted_FileName = "";
        public static int CompressingPercentDone = 0;

        public static string ErrMsg = "";

        public static bool Compress7Z(string filePath, string outPath)
        {
            bool IsDir=true;
            if (File.Exists(filePath)) IsDir=false;
            SevenZipCompressor.SetLibraryPath(Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"7z.dll"));
            SevenZipCompressor cmp = new SevenZipCompressor
                                         {
                                             ArchiveFormat = OutArchiveFormat.SevenZip,
                                             CompressionMethod = CompressionMethod.Lzma,
                                             CompressionMode = CompressionMode.Create,
                                             CompressionLevel = compressionLevel,
                                             VolumeSize = volumeSize 
                                         };
            //cmp.FileCompressionStarted+=new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
            //cmp.Compressing+=new EventHandler<ProgressEventArgs>(cmp_Compressing);
            //cmp.FilesFound+=new EventHandler<IntEventArgs>(cmp_FilesFound);

            //MainForm.ProgressBarMax = 100;
            //MainForm.ProgressBarMin = 0;
            //MainForm.ProgressBarValue = 0;
            //MainForm.ProgressBarVisible = true;
            MainForm.StatusText = lang.GetString("Please wait...");
            
            try
            {
                if (IsDir)
                    cmp.CompressDirectory(filePath, outPath, true);
                else
                    cmp.CompressFiles(outPath, filePath);
                    
                MainForm.StatusText = lang.GetString("Ready");
                //MainForm.ProgressBarVisible = false;
                //progress.Close();
                //progress.Dispose();
                //progress = null;
            }
            catch
            {
                MainForm.StatusText = "Ready";
                //MainForm.ProgressBarVisible = false;
                //progress.Close();
                //progress.Dispose();
                progress = null;
                return false;
            }

            return true;
                
        }
        //Archiver.C7Z.Compress7Z(@"C:\test\test.txt", @"C:\test\test.7z");
        public static void SetCompressionLevel(int cl)
        {
            switch (cl)
            {
                case 0:
                    compressionLevel = CompressionLevel.Fast;
                    break;
                case 1:
                    compressionLevel = CompressionLevel.High;
                    break;
                case 2:
                    compressionLevel = CompressionLevel.Low;
                    break;
                case 3:
                    compressionLevel = CompressionLevel.None;
                    break;
                case 4:
                    compressionLevel = CompressionLevel.Normal;
                    break;
                case 5:
                    compressionLevel = CompressionLevel.Ultra;
                    break;
            }
        }
        private static void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
        {
            if (progress == null)
            {
                progress = new ProgressForm(0, 100);
                progress.Text = e.FileName;
                progress.SetProgressBarValue((int)e.PercentDone);
                progress.ShowDialog();
            }
            else
            {
                progress.Text = e.FileName;
                progress.SetProgressBarValue((int)e.PercentDone);

            }

        }
        private static void cmp_Compressing(object sender, ProgressEventArgs e)
        {
            if (progress != null) progress.SetProgressBarValue((int)e.PercentDone);
        }
        private static void cmp_FilesFound(object sender, IntEventArgs e)
        {
            //G.main.ProgressBarValue(e.PercentDone);
        }

        public static string[] GetCompressFileNames(string sourceArc)
        {

            string[] ret = null;
            SevenZipCompressor.SetLibraryPath(Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"7z.dll"));
            SevenZipExtractor tmp;
            try
            {
                tmp = new SevenZipExtractor(sourceArc);

                if (!tmp.Check())
                {
                    tmp.Dispose();
                    return ret;
                }

                ret = new string[tmp.ArchiveFileNames.Count];
                int i = 0;
                foreach (string s in tmp.ArchiveFileNames)
                {
                    ret[i++] = s;
                }
                return ret;
            }
            catch
            {
                return ret;
            }
        }

        public static bool ExtractingFile(string sourceArc, string Targetpath)
        {
            MainForm.StatusText = lang.GetString("Please wait...");
            SevenZipCompressor.SetLibraryPath(Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"7z.dll"));
            SevenZipExtractor tmp;
            try
            {
                tmp = new SevenZipExtractor(sourceArc);
                
                if (!tmp.Check())
                {
                    tmp.Dispose();
                    //G.main.ErrLog(string.Format("壓縮檔損壞: {0}", sourceArc));
                    MainForm.StatusText = "Ready";
                    return false;
                }
            }
            catch
            {

                //G.main.ErrLog(string.Format("解開錯誤: {0}:{1}", sourceArc, ex.Message));
                MainForm.StatusText = "Ready";
                return false;
            }

            try
            {
                //G.main.ProgressBarSetup(100);
                tmp.FileExtractionStarted+=new EventHandler<FileInfoEventArgs>(tmp_FileExtractionStarted);
                tmp.Extracting += new EventHandler<ProgressEventArgs>(tmp_Extracting);
                tmp.ExtractArchive(Targetpath);
                tmp.Dispose();
                //G.main.ProgressBarHide();
                progress = null;
            }
            catch
            {
                //G.main.ErrLog(string.Format("解開錯誤: {0}:{1}", sourceArc, ex.Message));
                tmp.Dispose();
                MainForm.StatusText = "Ready";
                progress = null;
                return false;
            }
            MainForm.StatusText = "Ready";
            return true;
        }
        private static void tmp_Extracting(object sender, ProgressEventArgs e)
        {
            if (progress != null) progress.SetProgressBarValue((int)e.PercentDone);
        }
        private static void tmp_FileExtractionStarted(object sender, FileInfoEventArgs e)
        {
            if (progress == null)
            {
                progress = new ProgressForm(0, 100);
                progress.Text = e.FileInfo.FileName;
                progress.SetProgressBarValue((int)e.PercentDone);
                progress.ShowDialog();
            }
            else
            {
                progress.Text = e.FileInfo.FileName;
                progress.SetProgressBarValue((int)e.PercentDone);

            }
        }
    }

    
}
